#include "CTween.h"
#include "fastmath.h"

Int32 CALLBACKTYPE GetBackValue(
    /* [in] */ Int32 fromValue,
    /* [in] */ Int32 toValue,
    /* [in] */ Millisecond32 elaspedTime,
    /* [in] */ Millisecond32 durationTime,
    /* [in] */ EaseType easeType)
{
    double dx = toValue - fromValue;
    double delta = 0;
    double ratio = ((double)elaspedTime) / durationTime;
    double s = 1.70158;

    switch (easeType) {
    case EaseType_In:
        delta = dx * ratio * ratio *((s + 1) * ratio - s);

        break;

    case EaseType_Out:
        ratio -= 1.0;
        delta = dx * (ratio * ratio * ((s + 1) * ratio + s) + 1);
        break;

    case EaseType_InOut:
        ratio *= 2.0;
        s *= 1.525;

        if ((elaspedTime << 1) < durationTime) {
            delta = dx * 0.5 * (ratio * ratio * ((s + 1) * ratio - s));
        }
        else {
            ratio -= 2.0;
            delta = dx * 0.5 * (ratio * ratio * ((s + 1) * ratio + s) + 2);
        }

        break;

    default:
        assert(0);
    }

    return (Int32)(fromValue + delta);
}
